//
//  T3FAnnotation+Valuation.swift
//  t3_map
//
//  Created by Cunhai Li on 2025/1/22.
//

import Foundation
import SnapKit
import t3_assets

class T3FValuationAnnotationView: UIView, BubbleUpdateProtocol {
    /// 响应点击事件
    var annotationDidClicked: (([String : Any]?) -> Void)?

    private var option: T3FBubbleOption
    
    private let button: UIButton = UIButton(type: .custom)

    private let timeLabel: UILabel
    private let timeTipsLabel: UILabel
    private let timeStackView = UIStackView()

    private let contentLabel: UILabel
    private let stopInfoLabel: UILabel
    private let addressStackView = UIStackView()

    private let line: UIView
    private let arrowImageView: UIImageView

    deinit {
        print("ValuationBubbleView deinit")
    }

    init(option: T3FBubbleOption) {

        self.option = option

        contentLabel = LabelCreator.createLabel(options: option.contentText)
        contentLabel.numberOfLines = 2

        timeLabel = LabelCreator.createLabel(options: option.timeTextOption)
        timeLabel.numberOfLines = 1
        timeLabel.textAlignment = .center
        timeLabel.isHidden = true

        timeTipsLabel = LabelCreator.createLabel(options: option.timeTipsTextOption)
        timeTipsLabel.textAlignment = .center
        timeTipsLabel.numberOfLines = 1
        timeTipsLabel.isHidden = true

        stopInfoLabel = LabelCreator.createLabel(options: option.stopInfoTextOption)
        stopInfoLabel.isHidden = true

        line = UIView()
        line.backgroundColor = T3FColorUtil.hex("#EFF1F5")

        arrowImageView = UIImageView()
        arrowImageView.image = UIImage.t3_assetsWith(T3AssetsName.icon_right)

        super.init(frame: .zero)
        backgroundColor = .white
        timeStackView.isUserInteractionEnabled = false
        addressStackView.isUserInteractionEnabled = false

        addSubview(button)
        button.addTarget(self, action: #selector(buttonDidClicked), for: .touchUpInside)

        timeStackView.axis = .vertical
        addSubview(timeStackView)
        timeStackView.addArrangedSubview(timeLabel)
        timeStackView.addArrangedSubview(timeTipsLabel)

        addressStackView.axis = .vertical
        addSubview(addressStackView)
        addressStackView.addArrangedSubview(contentLabel)
        addressStackView.addArrangedSubview(stopInfoLabel)

        addSubview(line)
        addSubview(arrowImageView)

        updateWithOption(bubbleOptions: option)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        for subView in subviews {
            if subView.isUserInteractionEnabled {
                let subViewPoint = subView.convert(point, from: self)
                if subView.bounds.contains(subViewPoint) {
                    return true
                }
            }
        }
        return super.point(inside: point, with: event)
    }
    
    /// 气泡点击事件
    @objc func buttonDidClicked() {
        annotationDidClicked?(nil)
    }

    func updateWithOption(bubbleOptions: T3FBubbleOption?) {
        
        guard let bubbleOptions = bubbleOptions else { return }

        button.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }

        var contentLeft = 12.0

        if let timeTextOption = bubbleOptions.timeTextOption {
            
            timeStackView.snp.remakeConstraints { make in
                make.left.equalTo(12)
                make.top.equalTo(10)
                make.bottom.equalTo(-10)
                make.width.equalTo(44)
            }

            timeLabel.isHidden = false
            LabelCreator.updateLabel(label: timeLabel, options: timeTextOption)

            if let timeTipsTextOption = bubbleOptions.timeTipsTextOption {
                timeTipsLabel.isHidden = false
                LabelCreator.updateLabel(label: timeTipsLabel, options: timeTipsTextOption)
            }

            line.isHidden = false
            line.snp.remakeConstraints { make in
                make.left.equalTo(timeStackView.snp.right).offset(3)
                make.width.equalTo(1)
                make.top.equalTo(10)
                make.bottom.equalTo(-10)
            }
            contentLeft = 64.0
        } else {
            timeLabel.isHidden = true
            timeTipsLabel.isHidden = true
            line.isHidden = true
        }

        addressStackView.snp.remakeConstraints { make in
            make.top.equalTo(8)
            make.bottom.equalTo(-8)
            make.left.equalTo(contentLeft)
            make.width.lessThanOrEqualTo(90)
            make.right.equalTo(bubbleOptions.showArrow ? -24 : -12)
            make.centerY.equalToSuperview()
        }

        LabelCreator.updateLabel(label: contentLabel, options: bubbleOptions.contentText)

        if let stopInfo = bubbleOptions.stopInfoTextOption {
            stopInfoLabel.isHidden = false
            LabelCreator.updateLabel(label: stopInfoLabel, options: stopInfo)
        } else {
            stopInfoLabel.isHidden = true
        }

        if bubbleOptions.showArrow {
            arrowImageView.isHidden = false
            arrowImageView.snp.remakeConstraints { make in
                make.right.equalTo(-8)
                make.width.height.equalTo(12)
                make.centerY.equalToSuperview()
            }
        } else {
            arrowImageView.isHidden = true
        }
    }
}

